﻿
Namespace PL.CRMSDK
    Public Class PurchaseOrder_PL
        Inherits PLBase

        'Nos paramêtre pour controler si on génère les séquences ou non...
        Public Property SequenceGeneration As Boolean
        Private Property _defaultSociety As Guid
        Private Property _sessionSociety As Guid
        'Public aPayementNoSEQ As Boolean = False

        Public Sub New()
            MyBase.New()
            SequenceGeneration = True
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
            SequenceGeneration = True
            _defaultSociety = AppOption.DefaultSociety
            _sessionSociety = AppOption.SessionSociety
        End Sub

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            'Mandatory Fields
            'TODO: Cache MetaString in AppManager, so here we read it once maybe for nothing
            'Dim ms As MetaString
            'ms = MetaString.GetMetaStringByCode("Mandatory", AppManager.ApplicationOption.LanguageId)

            'Comme la SP va le générer on valide seulement si aOrderNoSEQ = False
            If Not IsDBNull(row("PurchaseOrderCd")) AndAlso SequenceGeneration = False Then
                row.SetColumnError("PurchaseOrderCd", "Mandatory")
            End If
            '
            ' We make sure we have a value in society
            ' This should not be overrriden
            If IsDBNull(row("SocietyID_FK")) Then
                If Not IsNothing(AppOption.SessionSociety) Then
                    row("SocietyID_FK") = AppOption.SessionSociety
                ElseIf Not IsNothing(AppOption.DefaultSociety) Then
                    row("SocietyID_FK") = AppOption.DefaultSociety
                Else
                    row.SetColumnError("Society", "Mandatory")
                End If
            End If


            If IsDBNull(row("SiteID_FK")) Then
                'According to row("SocietyID_FK") check for a default site and assign it 
                Using aMQ As MetaQuery = MetaQuery.GetMetaQueryByName("DefaultSite")
                    aMQ.SQLWhere = String.Format("SocietyID_FK= '{0}'", row("SocietyID_FK"))
                    aMQ.FillResultQuery()
                    If aMQ.ResultQuery.Rows.Count = 1 Then
                        row("SiteID_FK") = aMQ.ResultQuery.Rows(0)("SiteID")
                    End If
                End Using
            End If

            ' From the society we will get the current Tax rate , WARRING it is the current value in Society not Year or Period releated 
            '[SalesTaxRate1],[SalesTaxRate2], if one of them is null we go get the Society value 

            If IsDBNull(row("SalesTaxRate1")) Or IsDBNull(row("SalesTaxRate2")) Then
                Using Society As MetaQuery = MetaQuery.GetMetaQueryByName("Society")
                    Society.SQLWhere = String.Format("SocietyID = '{0}'", row("SocietyID_FK").ToString)
                    Society.FillResultQuery()
                    If Society.ResultQuery.Rows.Count > 0 Then
                        If IsDBNull(row("SalesTaxRate1")) Then
                            If IsDBNull(Society.ResultQuery.Rows(0)("Tax1")) Then
                                row("SalesTaxRate1") = 0
                            Else
                                row("SalesTaxRate1") = Society.ResultQuery.Rows(0)("Tax1")
                            End If
                        End If
                        If IsDBNull(row("SalesTaxRate2")) Then
                            If IsDBNull(Society.ResultQuery.Rows(0)("Tax2")) Then
                                row("SalesTaxRate2") = 0
                            Else
                                row("SalesTaxRate2") = Society.ResultQuery.Rows(0)("Tax2")
                            End If
                        End If

                    End If
                End Using
            End If






            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Public Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
            If e.Row.Table.TableName = "PurchaseOrder" Then

                'Dim afield As Guid = IIf(IsDBNull(e.Row("SocietyID_FK")), Nothing, e.Row("SocietyID_FK"))


                'Pour le Save sur un ADD on assign la sequence
                Me.MetaDataTable.Adapter.InsertCommand.Parameters("@OrderNoSEQ").Value = SequenceGeneration
                'Me.MetaDataTable.Adapter.InsertCommand.Parameters("@PayementNoSEQ").Value = aPayementNoSEQ
                '
                Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@OrderNoSEQ").Value = SequenceGeneration
                'Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@PayementNoSEQ").Value = aPayementNoSEQ

            End If
        End Sub

        Public Overrides Function Delete() As Boolean
            'A PO completed shall not be deleted , it should be ARCHIVE

            'A PO Open can be deleted with the related InventoryBatch 

            Return False
        End Function


    End Class
End Namespace
